﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using XMLGener.Utils;

namespace XMLGener.Data
{
    /// <summary>
    /// 
    /// </summary>
    public class XMLGenerTextNode:XMLGenerRangeElement
    {
        /// <summary>
        /// 
        /// </summary>
        private XMLGenerTextNode()
            : base()
        {
            this.xtype = XNodeType.TEXTNODE;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="parent"></param>
        public XMLGenerTextNode(String name, XMLGenerNode parent)
            : base(name, parent)
        {
            this.xtype = XNodeType.TEXTNODE;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="xmlElem"></param>
        public XMLGenerTextNode(XMLGenerNode parent, XmlElement xmlElem)
            : base(parent,xmlElem)
        {
            this.xtype = XNodeType.TEXTNODE;
            
            //this.name = xmlElem.GetAttribute("name");

        }

        /// <summary>
        /// 
        /// </summary>
        protected override void defaultInitialization()
        {
            base.defaultInitialization();
            this.nodeTypeStr = XMLGenerNode.XTEXTNODE_STR;
           
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="node"></param>
        public override void addChildNode(XMLGenerNode node)
        {
            return;
        }

        /// <summary>
        /// Return avialable types for this type of node
        /// </summary>
        /// <returns>list of strings</returns>
        public override XDataTypeCollection listAvailabeTypes()
        {
            if (this.ownerDocument != null)
                return this.ownerDocument.getAllDataTypes();
            return XMLGenerDataTypes.getDefaultTypes();           
            /*ArrayList result = new ArrayList();
            result.Add("any");
            result.Add("string");
            result.Add("number");
            result.Add("float");
            result.Add("date");
            return result;*/
        }

        //---------------------------
        // attributes from xml
        //---------------------------
        

        /// <summary>
        /// Indicator for using depth boundaries.
        /// </summary>
        public override bool useDepth
        {
            get { return false; }
            set { ; }
        }

        /// <summary>
        /// Minimal depth
        /// </summary>
        public override int minDepth
        {
            get { return 0; }
            set { ; }
        }

        /// <summary>
        /// Maximal depth
        /// </summary>
        public override int maxDepth
        {
            get { return 0; }
            set { ; }
        }


        /// <summary>
        /// Indicator for using fanout
        /// </summary>
        public override bool useFanout
        {
            get { return false; }
            set { ; }
        }

        /// <summary>
        /// Fanout value = count of child elements
        /// </summary>
        public virtual decimal fanout
        {
            get { return 0; }
            set { ; }
        }

        //---------------------------
        // generating 
        //---------------------------

        /// <summary>
        /// 
        /// </summary>
        public override bool initModule()
        {
            if (modInst != null)
                return true;
            if (this.module == null)
            {
                logger.addLog(this.GetType().ToString(), "Text node " + this.name + " (under element " + parentNode.name + ") has not defined generator module.");
                return true;
            }
            this.modInst = XModuleManager.getModuleInstance(this.module);
            if (this.modInst == null)
                return false;
            if (settingsElement != null)
                this.modInst.setParameters(getSettingsForModule(), ownerDocument);
            return true;
        }

        private string getSettingsForModule()
        {
            string result = "";
            if (settingsElement != null)
                result = settingsElement.OuterXml;
            if (this.ownerDocument != null)
                result = result + this.ownerDocument.getRestictionForDataType(this.contentType);


            return "<modulesettings>" + result + "</modulesettings>";
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string getGeneratedString()
        {
            if (this.modInst == null)
                return null;
            return modInst.generate();
        }

    }
}
